package com.bytedance.common.plugin.framework.model;

import android.net.Uri;
import android.os.AsyncTask;
import android.text.TextUtils;

import com.bytedance.common.plugin.framework.PluginManager;
import com.bytedance.common.plugin.framework.update.DownloadStatus;
import com.bytedance.common.plugin.framework.util.PluginUtil;
import com.bytedance.common.utility.Logger;

import org.json.JSONObject;

import java.io.File;
import java.util.List;

public final class InstallPluginItem extends PluginItem {
    public String apkUri;
    public String proxyClass;
    public String pluginClass;
    public String apkMD5;
    public ResourceType resourceType;
    public ProcessType processType;
    public String processNameSuffix;
    public List processNameSuffixList;
    public Integer contentLength;
    public String etag;
    public String downloadUrl = null;
    public DownloadStatus downloadStatus = DownloadStatus.STATUS_PENDING;
    public AsyncTask mAsyncTask;

    private InstallPluginItem(String packageName, String apkUri, String proxyClass, String pluginClass,
                              Integer apkVersionCode, String apkMD5, ResourceType resourceType,
                              ProcessType processType, String processNameSuffix, List processNameSuffixList) {
        super(packageName, apkVersionCode);
        this.apkUri = apkUri;
        this.proxyClass = proxyClass;
        this.pluginClass = pluginClass;
        this.apkMD5 = apkMD5;
        this.resourceType = resourceType;
        this.processType = processType;
        this.processNameSuffix = processNameSuffix;
        this.processNameSuffixList = processNameSuffixList;
    }

    public static InstallPluginItem fromJO(JSONObject jo) {
        InstallPluginItem remotePluginItem = null;
        if (jo == null) {
            return null;
        }

        try {
            String packageName = jo.optString("package_name");
            String apkUri = jo.optString("apk_uri");
            String proxyClass = jo.optString("proxy_class");
            String pluginClass = jo.optString("plugin_class");
            Integer apkVersionCode = Integer.valueOf(jo.optInt("apk_version_code"));
            String apkMd5 = jo.optString("apk_md5");
            Integer resourcesType = Integer.valueOf(jo.optInt("resources_type"));
            ResourceType resourceType = ResourceType.APP;
            if (resourcesType.intValue() == ResourceType.PLUGIN.getTypeValue()) {
                resourceType = ResourceType.PLUGIN;
            }

            Integer processTypeInt = Integer.valueOf(jo.optInt("process_type"));
            ProcessType processType;
            if (processTypeInt.intValue() == ProcessType.WORK.getTypeValue()) {
                processType = ProcessType.WORK;
            } else if (processTypeInt.intValue() == ProcessType.MAIN.getTypeValue()) {
                processType = ProcessType.MAIN;
            } else {
                processType = ProcessType.OTHER;
            }

            remotePluginItem = new InstallPluginItem(packageName, apkUri,
                    proxyClass, pluginClass,
                    apkVersionCode, apkMd5,
                    resourceType, processType,
                    jo.optString("process_name_suffix"),
                    PluginUtil.toList(jo.optJSONArray("process_name_suffix_list")));
        } catch (Exception e) {
            return null;
        }

        try {
            remotePluginItem.contentLength = Integer.valueOf(jo.optInt("content_length"));
            remotePluginItem.etag = jo.optString("e_tag");
            remotePluginItem.downloadUrl = jo.optString("download_url");
            Integer downloadStatus = Integer.valueOf(jo.optInt("download_status"));
            DownloadStatus status = DownloadStatus.STATUS_PENDING;
            if (downloadStatus.intValue() == DownloadStatus.STATUS_RUNNING.getTypeValue()) {
                status = DownloadStatus.STATUS_RUNNING;
            } else if (downloadStatus.intValue() == DownloadStatus.STATUS_PAUSED.getTypeValue()) {
                status = DownloadStatus.STATUS_PAUSED;
            } else if (downloadStatus.intValue() == DownloadStatus.STATUS_FAILED.getTypeValue()) {
                status = DownloadStatus.STATUS_FAILED;
            } else if (downloadStatus.intValue() == DownloadStatus.STATUS_SUCCESSFUL.getTypeValue()) {
                status = DownloadStatus.STATUS_SUCCESSFUL;
            }
            remotePluginItem.downloadStatus = status;
        } catch (Exception e) {
        }

        return remotePluginItem;
    }

    private static boolean equalTxt(String left, String right) {
        return left == null || !left.equals(right);
    }

    public final String getDstFilePath(String downloadDir) {
        String v0;
        if (TextUtils.isEmpty(downloadDir)) {
            v0 = null;
        } else {
            StringBuilder sb = new StringBuilder(downloadDir);
            sb.append("/plugin_").append(this.apkMD5).append(".pl");
            v0 = sb.toString();
        }

        return v0;
    }

    public final JSONObject toJO() {
        JSONObject jo = new JSONObject();
        try {
            jo.put("package_name", this.packageName);
            jo.put("apk_uri", this.apkUri);
            jo.put("proxy_class", this.proxyClass);
            jo.put("plugin_class", this.pluginClass);
            jo.put("apk_version_code", this.apkVersionCode);
            jo.put("apk_md5", this.apkMD5);
            jo.put("resources_type", this.resourceType.getTypeValue());
            jo.put("process_type", this.processType.getTypeValue());
            jo.put("process_name_suffix", this.processNameSuffix);
            jo.put("process_name_suffix_list", PluginUtil.toJsonArray(this.processNameSuffixList));
            jo.put("content_length", this.contentLength);
            jo.put("e_tag", this.etag);
            jo.put("download_url", this.downloadUrl);
            jo.put("download_status", this.downloadStatus.getTypeValue());
        } catch (Exception e) {
        }

        return jo;
    }

    public final boolean a(PluginItem pluginItem) {
        if (pluginItem != null && ((pluginItem instanceof InstallPluginItem))) {
            InstallPluginItem installPluginItem = (InstallPluginItem) pluginItem;
            if (!InstallPluginItem.equalTxt(this.proxyClass, installPluginItem.proxyClass)
                    && !InstallPluginItem.equalTxt(this.pluginClass, installPluginItem.pluginClass)
                    && this.resourceType == installPluginItem.resourceType
                    && this.processType == installPluginItem.processType) {
                if (installPluginItem.apkVersionCode.intValue() <= this.apkVersionCode.intValue()
                        && this.apkMD5 != null
                        && !this.apkMD5.equals(installPluginItem.apkMD5)) {
                    try {
                        if (!TextUtils.isEmpty(installPluginItem.apkUri)) {
                            Uri uri = Uri.parse(installPluginItem.apkUri);
                            if ("file".equals(uri.getScheme())) {
                                File file = new File(uri.getPath());
                                if (file.exists()) {
                                    file.delete();
                                }
                            }
                        }
                    } catch (Exception e) {
                    }
                    return true;
                }
                return false;
            } else {
                return false;
            }

        } else {
            return true;
        }
    }

    public final boolean b() {
        boolean v0_1;
        if (!super.b()
                || (TextUtils.isEmpty(this.proxyClass))
                || (TextUtils.isEmpty(this.pluginClass))
                || (TextUtils.isEmpty(this.apkMD5))) {
            v0_1 = false;
        } else {
            if (!TextUtils.isEmpty(this.packageName)
                    && !TextUtils.isEmpty(this.proxyClass)
                    && this.resourceType != null
                    && this.processType != null) {
                Object v0 = com.bytedance.common.plugin.a.d.get(this.packageName);
                if (v0 == null) {
                    v0_1 = false;
                } else {
                    if ((this.packageName.equals(((com.bytedance.common.plugin.a.a$a) v0).packageName))
                            && (this.proxyClass.equals(((com.bytedance.common.plugin.a.a$a) v0).proxyClass))
                            && this.resourceType == ((com.bytedance.common.plugin.a.a$a) v0).resourceType
                            && this.processType == ((com.bytedance.common.plugin.a.a$a) v0).processType) {
                        return true;
                    }

                    v0_1 = false;
                }

                return v0_1;
            }

            v0_1 = false;
        }

        return v0_1;
    }

    protected final String getSpKey() {
        return "plugin_update_" + this.packageName;
    }

    public final boolean isReady() {
        String v4;
        File v3;
        String v2_1;
        boolean v0 = true;
        try {
            if (Logger.debug()) {
                Logger.d("PluginUpdateHelper", "isReady downloadStatus = " + this.downloadStatus);
            }

            if (this.downloadStatus != DownloadStatus.STATUS_SUCCESSFUL) {
                return false;
            }

            Plugin v2 = PluginManager.getPlugin(this.packageName);
            if (Logger.debug()) {
                Logger.d("PluginUpdateHelper", "isReady plugin = " + v2);
            }

            if (v2 != null) {
                v2_1 = v2.apkPath + "/" + v2.fileName();
                v3 = new File(v2_1);
                v4 = PluginUtil.fileToMD5(v3);
                if (Logger.debug()) {
                    Logger.d("PluginUpdateHelper", "PluginPath = " + v2_1 + " PluginMd5 = " + v4);
                }

                if (!v3.exists()) {
                } else if (!this.apkMD5.equals(v4)) {
                } else {
                    return v0;
                }
            }

            if (TextUtils.isEmpty(this.apkUri)) {
                return false;
            }

            Uri v2_2 = Uri.parse(this.apkUri);
            if (Logger.debug()) {
                Logger.d("PluginUpdateHelper", "isReady uri = " + v2_2);
            }

            if (!"file".equals(v2_2.getScheme())) {
                return false;
            }

            v2_1 = v2_2.getPath();
            v3 = new File(v2_1);
            v4 = PluginUtil.fileToMD5(v3);
            if (Logger.debug()) {
                Logger.d("PluginUpdateHelper", "path = " + v2_1 + " fileMd5 = " + v4);
            }

            if (!v3.exists()) {
                return false;
            }

            if (this.apkMD5.equals(v4)) {
                return v0;
            }
        } catch (Exception v0_1) {
            v0_1.printStackTrace();
        }

        return false;
    }
}

